package com.zzjson.springbootjdbc.webflux;

import com.zzjson.springbootjdbc.domain.User;
import com.zzjson.springbootjdbc.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.reactive.function.server.RequestPredicates;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Flux;

import java.util.Collection;

import static org.springframework.web.reactive.function.server.RequestPredicates.POST;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2019 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : webflux 配置</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2019年11月04日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
@Configuration
public class WebFluxConfiguration {
	@Bean
	public RouterFunction<ServerResponse> saveUser(UserHandler userHandler) {
		return route(POST("/web/flux/user/save"), userHandler::save);
	}

	@Bean
	@Autowired
	public RouterFunction<ServerResponse> routerFunctionUsers(UserRepository userRepository) {
		Collection<User> users = userRepository.findAll();
		Flux<User> userFlux = Flux.fromIterable(users);
		// Mono<Collection<User>> mono = Mono.just(users);
		RouterFunction<ServerResponse> route = route(RequestPredicates.path("/users"), request -> {
			return ServerResponse.ok().body(userFlux, User.class);
		});
		return route;
	}
}